package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.bo.goodsbo.TopicGoodsBO;
import com.cskaoyan.bean.common.BasePageInfoTopic;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.vo.TopicVo;
import com.cskaoyan.service.promote.TopicService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.controller.promote
 * @CreateTime: 2022-11-21  15:39
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    public BaseRespVo topicList(BasePageInfoTopic basePageInfoTopic) {
        PageInfo<MarketTopic> pageInfo = topicService.selectByBasePage(basePageInfoTopic);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.succeed(data);
    }

    @RequestMapping("create")
    public BaseRespVo topicCreate(@RequestBody MarketTopic marketTopic) {

        Date date = new Timestamp(System.currentTimeMillis());
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);

        topicService.insertByMarketTopic(marketTopic);
        return BaseRespVo.succeed(marketTopic);
    }

    @PostMapping("batch-delete")
    public BaseRespVo topicBatchDelete(@RequestBody Map map) {
        ArrayList<Integer> ids = (ArrayList<Integer>) map.get("ids");
        topicService.deleteByIds(ids);
        return BaseRespVo.succeed();
    }

    @RequestMapping("delete")
    public BaseRespVo topicDelete(@RequestBody MarketTopic marketTopic) {
        topicService.deleteById(marketTopic.getId());
        return BaseRespVo.succeed();
    }

    @RequestMapping("update")
    public BaseRespVo topicUpdate(@RequestBody MarketTopic marketTopic) {

        Date date = new Timestamp(System.currentTimeMillis());
        marketTopic.setUpdateTime(date);

        topicService.updateByMarketTopic(marketTopic);
        return BaseRespVo.succeed(marketTopic);
    }

    @RequestMapping("read")
    public BaseRespVo topicRead(Integer id) {
        MarketTopic marketTopic = topicService.selectById(id);

        String[] goods = marketTopic.getGoods();
        int[] ids = new int[goods.length];
        for (int i = 0; i < goods.length; i++) {
            ids[i]=Integer.valueOf(goods[i]);
        }

        List<TopicGoodsBO> list = topicService.selectGoodsById(ids);
        TopicVo topicVo = new TopicVo(list, marketTopic);
        return BaseRespVo.succeed(topicVo);
    }


}